package com.tkjt.pms.modules.datainitail.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.*;
import com.tkjt.pms.modules.common.mapper.TkDataInitialMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexInfoMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueAuditMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueMapper;
import com.tkjt.pms.modules.datainitail.mapper.TkIndexValueInitialMapper;
import com.tkjt.pms.modules.datainitail.service.TkDataInitialService;
import com.tkjt.pms.modules.manageWeight.mapper.WeightMapper;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("tkDataInitialService")
@Transactional
public class TkDataInitialServiceImpl implements TkDataInitialService {

    @Autowired
    TkDataInitialMapper tkDataInitialMapper;
    @Autowired
    WeightMapper weightMapper;
    @Autowired
    TkIndexValueMapper tkIndexValueMapper;
    @Autowired
    TkIndexInfoMapper tkIndexInfoMapper;
    @Autowired
    SysDepartMapper sysDepartMapper;
    @Autowired
    TkIndexValueAuditMapper tkIndexValueAuditMapper;
    @Autowired
    TkIndexValueInitialMapper tkIndexValueInitialMapper;

    @Override
    @Transactional
    public void initialData(String weight,String target,String relax,String radio,String grade) {
        QueryWrapper<TkDataInitialEntity> initialQueryWrapper = new QueryWrapper<>();
        initialQueryWrapper.lambda().eq(TkDataInitialEntity::getCode, "valueInitial");
        TkDataInitialEntity dataInitialEntity = tkDataInitialMapper.selectOne(initialQueryWrapper);
        try{
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            QueryWrapper<TkIndexWeight> weightQueryWrapper = new QueryWrapper<>();
            if("1".equals(weight)){
                TkIndexWeight tkIndexWeightEntity = new TkIndexWeight();
                tkIndexWeightEntity.setWeightY(String.valueOf(year));
                tkIndexWeightEntity.setWeightM01("");
                tkIndexWeightEntity.setWeightM02("");
                tkIndexWeightEntity.setWeightM03("");
                tkIndexWeightEntity.setWeightM04("");
                tkIndexWeightEntity.setWeightM05("");
                tkIndexWeightEntity.setWeightM06("");
                tkIndexWeightEntity.setWeightM07("");
                tkIndexWeightEntity.setWeightM08("");
                tkIndexWeightEntity.setWeightM09("");
                tkIndexWeightEntity.setWeightM10("");
                tkIndexWeightEntity.setWeightM11("");
                tkIndexWeightEntity.setWeightM12("");
                weightMapper.update(tkIndexWeightEntity,weightQueryWrapper);
            }
            Map<String,Object> map = new HashMap<>();
            map.put("likeValueName","目标值");
            if("1".equals(target)){
                tkIndexValueInitialMapper.updateIndexValueByIndexValueName(map);
            }
            map.put("likeValueName","实际值");
            if("1".equals(relax)){
                tkIndexValueInitialMapper.updateIndexValueByIndexValueName(map);
            }
            map.put("likeValueName","达成");
            if("1".equals(radio)){
                tkIndexValueInitialMapper.updateIndexValueByIndexValueName(map);
            }
            map.put("likeValueName","得分");
            if("1".equals(grade)){
                tkIndexValueInitialMapper.updateIndexValueByIndexValueName(map);
            }
            //审核数据初始化
            QueryWrapper<TkIndexValueAudit> auditQueryWrapper = new QueryWrapper<>();
            TkIndexValueAudit tkIndexValueAudit = new TkIndexValueAudit();
            tkIndexValueAudit.setIndexValueY(String.valueOf(year));
            tkIndexValueAudit.setAuditStatus("1");
            tkIndexValueAuditMapper.update(tkIndexValueAudit,auditQueryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            dataInitialEntity.setStatus("4");
            tkDataInitialMapper.update(dataInitialEntity,initialQueryWrapper);
        }

        dataInitialEntity.setStatus("3");
        tkDataInitialMapper.update(dataInitialEntity,initialQueryWrapper);
    }

    @Override
    public void initialDataForNewUser() {
//        List<Map<String,Object>> userDeparts = tkIndexValueInitialMapper.selectUserAndDepartByParam();
//        Calendar cal = Calendar.getInstance();
//        int year = cal.get(Calendar.YEAR);
//        List<TkIndexValue> indexValueList = new ArrayList<>();
//        List<TkIndexReport> reportList = new ArrayList<>();
//        for(Map<String,Object> userDepart : userDeparts){
//            QueryWrapper<TkIndexValue> indexValueQueryWrapper = new QueryWrapper<>();
//            indexValueQueryWrapper.lambda().eq(TkIndexValue::getIndexExecutor,userDepart.get("userName").toString());
//            int count = tkIndexValueMapper.selectCount(indexValueQueryWrapper);
//            if(count == 0){
//                List<SysDepart> orgList = new ArrayList<>();
//                if("A01A01A01".equals(userDepart.get("orgCode").toString().substring(0,9))){
//                    QueryWrapper<SysDepart> departQueryWrapper = new QueryWrapper<>();
//                    departQueryWrapper.lambda().likeRight(SysDepart::getOrgCode,"A01A01A02A");
//                    departQueryWrapper.lambda().ne(SysDepart::getOrgGroup,"0");
//                    orgList = sysDepartMapper.selectList(departQueryWrapper);
//                    for(SysDepart depart : orgList){
//                        TkIndexValue tempIndexValue = new TkIndexValue();
//                        tempIndexValue.setYear(String.valueOf(year));
//                        tempIndexValue.setIndexCode("IAAB000");
//                        tempIndexValue.setIndexValueCode("KGIAAB017VA001");
//                        setDataForIndexValue(tempIndexValue,depart,userDepart);
//                        indexValueList.add(tempIndexValue);
//                    }
//                }
//                if("A01A01A02".equals(object[1].toString().substring(0,9))){
//                    orgList = this.findHql(" from TSDepart where orgCode like 'A01A01A01A%' and orgGroup != '0'");
//                    for(Object orgObj : orgList){
//                        TSDepart tempDepart = (TSDepart)orgObj;
//                        TkIndexValueEntity tempIndexValue1 = new TkIndexValueEntity();
//                        tempIndexValue1.setYear(String.valueOf(year));
//                        tempIndexValue1.setIndexCode("IAAB017");
//                        tempIndexValue1.setIndexValueCode("KCIAAB017VA001");
//                        setDataForIndexValue(tempIndexValue1,tempDepart,object);
//                        indexValueList.add(tempIndexValue1);
//                        TkIndexValueEntity tempIndexValue2 = new TkIndexValueEntity();
//                        tempIndexValue2.setYear(String.valueOf(year));
//                        tempIndexValue2.setIndexCode("IAAC005");
//                        tempIndexValue2.setIndexValueCode("KCIAAC005VB001");
//                        setDataForIndexValue(tempIndexValue2,tempDepart,object);
//                        indexValueList.add(tempIndexValue2);
//                    }
//                }
//            }
//            if("A01A01A01".equals(object[1].toString().substring(0,9))){
//                stringBuffer = new StringBuffer("SELECT id FROM TkIndexReportEntity where indexGroup='2' and reportCode='"+object[0].toString()+"'");
//                List<String> reportIds = this.findByQueryString(stringBuffer.toString());
//                if(reportIds.size() == 0){
//                    TkIndexReportEntity exampleEntity = new TkIndexReportEntity();
//                    exampleEntity.setId(UUID.randomUUID().toString());
//                    exampleEntity.setIndexGroup("2");
//                    exampleEntity.setReportCode(object[0].toString());
//                    exampleEntity.setReportName("我的绩效报表("+object[2].toString()+")");
//                    exampleEntity.setReportDate(new Date());
//                    exampleEntity.setReportStatus(1);
//                    exampleEntity.setDownloadDate(new Date());
//                    exampleEntity.setDownloadTimes(0);
//                    exampleEntity.setCreateBy("admin");
//                    exampleEntity.setCreateDate(new Date());
//                    exampleEntity.setUpdateBy("admin");
//                    exampleEntity.setUpdateDate(new Date());
//                    reportList.add(exampleEntity);
//                }
//            }
//            if("A01A01A02".equals(object[1].toString().substring(0,9))){
//                stringBuffer = new StringBuffer("SELECT id FROM TkIndexReportEntity where indexGroup='1' and reportCode='"+object[0].toString()+"'");
//                List<String> reportIds = this.findByQueryString(stringBuffer.toString());
//                if(reportIds.size() == 0){
//                    TkIndexReportEntity exampleEntity = new TkIndexReportEntity();
//                    exampleEntity.setId(UUID.randomUUID().toString());
//                    exampleEntity.setIndexGroup("1");
//                    exampleEntity.setReportCode(object[0].toString());
//                    exampleEntity.setReportName("我的绩效报表("+object[2].toString()+")");
//                    exampleEntity.setReportDate(new Date());
//                    exampleEntity.setReportStatus(1);
//                    exampleEntity.setDownloadDate(new Date());
//                    exampleEntity.setDownloadTimes(0);
//                    exampleEntity.setCreateBy("admin");
//                    exampleEntity.setCreateDate(new Date());
//                    exampleEntity.setUpdateBy("admin");
//                    exampleEntity.setUpdateDate(new Date());
//                    reportList.add(exampleEntity);
//                }
//            }
//        }
//        if(indexValueList.size() > 0){
//            this.batchSave(indexValueList);
//        }
//        if(reportList.size() > 0){
//            this.batchSave(reportList);
//        }
//        TkDataInitialEntity dataInitialEntity = this.singleResult("from TkDataInitialEntity where code = 'newUserData'");
//        dataInitialEntity.setStatus("3");
//        this.saveOrUpdate(dataInitialEntity);
    }

    public void setDataForIndexValue(TkIndexValue tempIndexValue, SysDepart tempDepart, Map<String,Object> userDepart){
        tempIndexValue.setIndexDepart(tempDepart.getOrgCode());
        tempIndexValue.setIndexExecutor(userDepart.get("userNmae").toString());
        tempIndexValue.setIndexExecutorDepart(userDepart.get("orgCode").toString());
        tempIndexValue.setMTotal(0.0D);
        tempIndexValue.setM01(0.0D);
        tempIndexValue.setM02(0.0D);
        tempIndexValue.setM03(0.0D);
        tempIndexValue.setM04(0.0D);
        tempIndexValue.setM05(0.0D);
        tempIndexValue.setM06(0.0D);
        tempIndexValue.setM07(0.0D);
        tempIndexValue.setM08(0.0D);
        tempIndexValue.setM09(0.0D);
        tempIndexValue.setM10(0.0D);
        tempIndexValue.setM11(0.0D);
        tempIndexValue.setM12(0.0D);

        tempIndexValue.setQ01(0.0D);
        tempIndexValue.setQ02(0.0D);
        tempIndexValue.setQ03(0.0D);
        tempIndexValue.setQ04(0.0D);

        tempIndexValue.setCreateBy("admin");
        tempIndexValue.setCreateDate(new Date());
        tempIndexValue.setUpdateBy("admin");
        tempIndexValue.setUpdateDate(new Date());
    }
}
